package leetcode.medium.removeduplicates;

import util.annotation.Solved;

/**
 * <a href="https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/">80. 删除有序数组的重复项 II</a>
 */
@Solved
public class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums.length <= 1) {
            return nums.length;
        }
        int i = 0, j = 0, count = 0;
        while (j < nums.length) {
            if (j == 0 || (nums[j] == nums[j - 1] && count < 2)) {
                nums[i++] = nums[j];
                count++;
            } else if (nums[j] != nums[j - 1]) {
                nums[i++] = nums[j];
                count = 1;
            }
            j++;
        }
        return i;
    }
}
